package com.wyp168.juc.future;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

/**
 * @ClassName complatableFutureDemo
 * @Description TODO
 * @Author wyp168
 * @Date 2022/9/9 23:27
 */
public class complatableFutureDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        long startTime = System.currentTimeMillis();
        FutureTask<String> t1 = new FutureTask<String>(() -> {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t1");
        FutureTask<String> t2 = new FutureTask<String>(() -> {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t2");
        FutureTask<String> t3 = new FutureTask<String>(() -> {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t3");

        ExecutorService threadPool = Executors.newCachedThreadPool();
        threadPool.submit(t1);
        threadPool.submit(t2);
        threadPool.submit(t3);

        while(true) {
            if (t1.isDone()) {
                break;
            }
            //t1线程没结束
            System.out.println("t1 线程忙着呢，不要催了，再催躺平");
            Thread.sleep(100);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("costTime---" + (endTime - startTime) + "毫秒");
    }
}
